﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WLFramework.Data.Linq;
using WLFramework.DataAccess;
using WLFramework.Security;

namespace SampleSite.admin
{
    public partial class application : KeyValuePage
    {
        public string AppId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            string appid = this.Request.QueryString["appid"].Replace(' ', '+');
            bool flag = false;
            if (!string.IsNullOrEmpty(appid))
            {
                int id = int.Parse(AES.AESDecrypt(appid));
                using (PermissionContext db = new PermissionContext())
                {
                    var cmd = DataAccessor.CreateInstance<PermissionCommand>(db);
                    var query = cmd.GetApplicationByUserID(this.LoginUser.U_ID);

                    

                    foreach (var app in query)
                    {
                        if (app.A_ID == id)
                        {
                            this.AppId = appid;
                            this.SetValue("ApplicationName", app.A_CName);
                            flag = true;
                        }
                    }

                    /*
                    
                    var roles = from p in db.sys_UserRoles
                                where p.R_State == 1 && p.U_ID == this.LoginUser.U_ID
                                select p.R_ID;
                    
                    var app = (from p in db.sys_RoleApplication
                               where p.A_State == 1 && p.A_ID == id &&
                                    p.sysRoleApplicationsysApplications.A_State == 1 &&
                                    roles.Contains(p.R_ID)
                               select new
                               {
                                   p.sysRoleApplicationsysApplications.A_CName
                               }).FirstOrDefault();

                    if (app != null)
                    {
                        this.AppId = appid;
                        this.SetValue("ApplicationName", app.A_CName);
                        flag = true;
                    }
                    */
                }
            }

            if (!flag)
            {
                this.Response.Redirect("/admin/Errors.aspx");
            }
        }
    }
}